/*
 * Copyright (c) 2009. Silenus Consultoria, S.L.
 */
package es.silenus.detecta.controller;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Errors bean.
 *
 * @author <a href="mailto:malonso@silenus-consultoria.es">Mariano Alonso</a>
 *
 * @since 31-mar-2009 20:04:17
 */
public class Errors implements Serializable {
	/**
	 * The global errors.
	 */
	private final List<String> globals;

	/**
	 * The fields errors.
	 */
	private final Map<String, String> fields;

/**
   * Constructor.
   */
	public Errors() {
		globals = new ArrayList<String>();
		fields = new HashMap<String, String>();
	}

	/**
	 * Adds a global error message.
	 *
	 * @param globalError a global error message.
	 */
	public void add(final String globalError) {
		globals.add(globalError);
	}

	/**
	 * Adds a field error.
	 *
	 * @param field a field.
	 * @param error the error message.
	 */
	public void add(final String field, final String error) {
		fields.put(field, error);
	}

	/**
	 * Retrieves the global errors.
	 *
	 * @return the global errors.
	 */
	public List<String> getGlobals() {
		return globals;
	}

	/**
	 * Retrieves the field errors.
	 *
	 * @return the fields.
	 */
	public Map<String, String> getFields() {
		return fields;
	}

	/**
	 * Checks if global errors exist.
	 *
	 * @return true if global errors exist, false otherwise.
	 */
	public boolean hasGlobalErrors() {
		return globals.size() > 0;
	}

	/**
	 * Checks if field errors exist.
	 *
	 * @return true if field errors exist, false otherwise.
	 */
	public boolean hasFieldErrors() {
		return fields.size() > 0;
	}

	/**
	 * Checks if has errors of any kind.
	 *
	 * @return true if errors exist, false otherwise.
	 */
	public boolean hasErrors() {
		return hasGlobalErrors() || hasFieldErrors();
	}
}
